﻿using System;
using System.Windows;

namespace ZOIL.Library.Behaviors
{
    public static class RoutedEventHelper
    {
        public static void AddHandler(DependencyObject element, RoutedEvent evt, Delegate handler)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            if (element is UIElement)
            {
                (element as UIElement).AddHandler(evt, handler);
            }
            else if (element is ContentElement)
            {
                (element as ContentElement).AddHandler(evt, handler);
            }
            else
            {
                throw new ArgumentException("Provided element is no UIElement or ContentElement !", "element");
            }
        }

        public static void RemoveHandler(DependencyObject element, RoutedEvent evt, Delegate handler)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            if (element is UIElement)
            {
                (element as UIElement).RemoveHandler(evt, handler);
            }
            else if (element is ContentElement)
            {
                (element as ContentElement).RemoveHandler(evt, handler);
            }
            else
            {
                throw new ArgumentException("Provided element is no UIElement or ContentElement !", "element");
            }
        }
    }
}